package com.zline.foodraise.util;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.SystemService;

/*
 * 网络统一管理类
 * 
 * */
@EBean
public class NetworkUtil {
    private static final String LOG_TAG = NetworkUtil.class.getSimpleName();
    @SystemService
    ConnectivityManager connectivityManager;
    @SystemService
    WifiManager wifiManager;
    @SystemService
    TelephonyManager telephonyManager;

    //网络是否有用
    public boolean isNetworkAvailable() {
        if (connectivityManager == null)
            return false;
        NetworkInfo netinfo[] = connectivityManager.getAllNetworkInfo();
        if (netinfo == null)
            return false;
        for (int i = 0; i < netinfo.length; i++)
            if (netinfo[i].isConnected())
                return true;

        return false;
    }

    //wifi 是否连接
    public boolean isOpenWifi() {
        return wifiManager.isWifiEnabled();
    }

    //3G网络 是否连接
    public boolean is3gDataActive() {
        int state = telephonyManager.getDataState();
        switch (state) {
            case TelephonyManager.DATA_CONNECTING:
            case TelephonyManager.DATA_CONNECTED:
                return true;
            case TelephonyManager.DATA_DISCONNECTED:
                return false;
        }
        return false;
    }

}

